import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/modules/application/special_operation/special_list/job_plan_request/planning_request/planning_request_controller.dart';
import 'package:jiffy/jiffy.dart';

/// 计划申请页面
class PlanningRequestWidget extends StatelessWidget {
  PlanningRequestWidget({super.key});

  final controller = Get.put(PlanningRequestController());

  // 输入框
  Widget _inputWidget(String title, String hintText, TextEditingController textController) {
    return Column(
      children: [
        Row(
          children: [
            AppGaps.hGap16,
            Text(
              '*',
              style: TextStyle(
                fontSize: 16.sp,
                color: AppColors.meaning_color_danger,
              ),
            ),
            Text(
              title,
              style: TextStyle(
                fontSize: 14.sp,
                color: AppColors.font_color_black,
              ),
            ),
            Expanded(
                child: TextField(
              enabled: controller.jobPlanRequestController.isEnabled,
              controller: textController,
              style: TextStyle(
                fontSize: 14.sp,
                color: AppColors.font_color_black,
              ),
              textAlign: TextAlign.right,
              textDirection: TextDirection.rtl,
              decoration: InputDecoration(
                hintText: hintText,
                hintStyle: TextStyle(
                  fontSize: 14.sp,
                  color: AppColors.text_grey,
                ),
                border: InputBorder.none,
                contentPadding: EdgeInsets.symmetric(horizontal: 10.w),
              ),
            )),
            AppGaps.hGap16,
          ],
        ),
        Container(
            padding: EdgeInsets.symmetric(horizontal: 16.w),
            child: Divider(
              height: 1.w,
              color: AppColors.bg_color_grey,
            )),
      ],
    );
  }

  // 选择框 [callback] 选择回调
  Widget _selectWidget(String title, String hintText, BuildContext context, {required Function() callback}) {
    return Column(
      children: [
        Row(
          children: [
            AppGaps.hGap16,
            Text(
              '*',
              style: TextStyle(
                fontSize: 16.sp,
                color: AppColors.meaning_color_danger,
              ),
            ),
            Text(
              title,
              style: TextStyle(
                fontSize: 14.sp,
                color: AppColors.font_color_black,
              ),
            ),
            Expanded(
              child: SizedBox(
                height: 50.w,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    Text(
                      hintText == '' ? '请选择' : hintText,
                      style: TextStyle(
                        fontSize: 14.sp,
                        color: AppColors.text_grey,
                      ),
                    ),
                    AppGaps.hGap10,
                    Visibility(
                      visible: controller.jobPlanRequestController.isEnabled,
                      child: Icon(Icons.arrow_forward_ios, size: 14.sp, color: AppColors.text_grey),
                    ),
                  ],
                ),
              ),
            ),
            AppGaps.hGap16,
          ],
        ),
        Container(
            padding: EdgeInsets.symmetric(horizontal: 16.w),
            child: Divider(
              height: 1.w,
              color: AppColors.bg_color_grey,
            )),
      ],
    ).click(() {
      if (!controller.jobPlanRequestController.isEnabled) {
        return;
      }
      FocusScope.of(context).unfocus();
      callback();
    });
  }

  // 作业地点选择框 [callback] 选择回调
  Widget _jobSelectWidget(String title, String hintText, {required Function() callback}) {
    return Column(
      children: [
        AppGaps.vGap16,
        Row(
          children: [
            AppGaps.hGap16,
            Text(
              '*',
              style: TextStyle(
                fontSize: 16.sp,
                color: AppColors.meaning_color_danger,
              ),
            ),
            Text(
              title,
              style: TextStyle(
                fontSize: 14.sp,
                color: AppColors.font_color_black,
              ),
            ),
            Expanded(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  Expanded(
                    child: Text(
                      hintText == '' ? '请选择' : hintText,
                      textAlign: TextAlign.right,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                        fontSize: 14.sp,
                        color: AppColors.text_grey,
                      ),
                    ),
                  ),
                  AppGaps.hGap10,
                  Visibility(
                    visible: controller.jobPlanRequestController.isEnabled,
                    child: Icon(Icons.arrow_forward_ios, size: 14.sp, color: AppColors.text_grey),
                  ),
                ],
              ),
            ),
            AppGaps.hGap16,
          ],
        ),
        AppGaps.vGap6,
        Align(
          alignment: Alignment.centerLeft,
          child: Container(
            padding: EdgeInsets.symmetric(vertical: 8.w, horizontal: 10.w),
            margin: EdgeInsets.only(left: 16.w),
            decoration: BoxDecoration(color: const Color(0x1AFD6B6B), borderRadius: BorderRadius.circular(4.r)),
            child: Text(
              '提示：(禁止在同一地点进行两处或两处以上的作业)',
              style: TextStyle(color: AppColors.text_red, fontSize: 10.sp),
            ),
          ),
        ),
        AppGaps.vGap8,
        Container(
            padding: EdgeInsets.symmetric(horizontal: 16.w),
            child: Divider(
              height: 1.w,
              color: AppColors.bg_color_grey,
            )),
      ],
    ).click(() {
      if (!controller.jobPlanRequestController.isEnabled) {
        return;
      }
      callback();
    });
  }

  // 作业内容
  Widget _jobContentWidget(String title) {
    return Column(
      children: [
        AppGaps.vGap16,
        Row(
          children: [
            AppGaps.hGap16,
            Text(
              '*',
              style: TextStyle(
                fontSize: 16.sp,
                color: AppColors.meaning_color_danger,
              ),
            ),
            Text(
              '作业内容',
              style: TextStyle(
                fontSize: 14.sp,
                color: AppColors.font_color_black,
              ),
            ),
          ],
        ),
        AppGaps.vGap12,
        Container(
          padding: EdgeInsets.symmetric(horizontal: 16.w),
          child: Container(
            padding: EdgeInsets.symmetric(vertical: 12.w),
            decoration: BoxDecoration(
              color: AppColors.bg_color_input,
              borderRadius: BorderRadius.circular(4.w),
              border: Border.all(
                color: AppColors.bg_color_grey,
                width: 1.w,
              ),
            ),
            child: TextField(
              enabled: controller.jobPlanRequestController.isEnabled,
              controller: controller.jobContentController,
              maxLines: 5,
              style: TextStyle(
                fontSize: 14.sp,
                color: AppColors.font_color_black,
              ),
              decoration: InputDecoration(
                hintText: '请输入',
                hintStyle: TextStyle(
                  fontSize: 14.sp,
                  color: AppColors.text_grey,
                ),
                border: InputBorder.none,
                contentPadding: EdgeInsets.symmetric(horizontal: 10.w),
              ),
            ),
          ),
        ),
        Container(
            padding: EdgeInsets.symmetric(horizontal: 16.w),
            child: Divider(
              height: 1.w,
              color: AppColors.bg_color_grey,
            )),
      ],
    );
  }

  // 计划作业开始时间选取
  _selectTime() async {
    final DateTime? startTime = await showDatePicker(
      context: Get.context!,
      initialDate: DateTime.now(),
      firstDate: DateTime(2000),
      lastDate: DateTime(2100),
      helpText: '请选择计划作业开始时间',
    );
    if (startTime != null) {
      controller.jobPlanDetailsEntity.planStartDate = Jiffy.parseFromDateTime(startTime).format(pattern: 'yyyy-MM-dd');
      controller.update();
      _selectEndTime();
    }
  }

  // 计划作业结束时间选取
  _selectEndTime() async {
    final DateTime? endTime =
        await showDatePicker(context: Get.context!, initialDate: DateTime.now(), firstDate: DateTime(2000), lastDate: DateTime(2100), helpText: '请选择计划作业结束时间');
    if (endTime != null) {
      controller.jobPlanDetailsEntity.planEndDate = Jiffy.parseFromDateTime(endTime).format(pattern: 'yyyy-MM-dd');
      controller.update();
    }
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            body: SingleChildScrollView(
              child: Column(
                children: [
                  Container(
                    height: 16.w,
                    color: AppColors.bg_color_grey,
                  ),
                  _inputWidget('作业名称', '请输入', controller.jobNameController),
                  _selectWidget(
                    '计划作业时间',
                    controller.jobPlanDetailsEntity.planStartDate == ''
                        ? '请选择'
                        : '${controller.jobPlanDetailsEntity.planStartDate}至${controller.jobPlanDetailsEntity.planEndDate}',
                    context,
                    callback: () {
                      _selectTime();
                    },
                  ),
                  _selectWidget(
                    '申请单位',
                    controller.jobPlanDetailsEntity.applyDepartmentName!,
                    context,
                    callback: () {
                      controller.applyUnitSelect();
                    },
                  ),
                  _jobContentWidget('作业内容'),
                  _jobSelectWidget(
                    '作业地点',
                    controller.jobPlanDetailsEntity.ticketAddress!,
                    callback: () {
                      FocusScope.of(context).unfocus();
                      controller.jobLocationSelect();
                    },
                  ),
                  _selectWidget(
                    '风险辨识人',
                    controller.jobPlanDetailsEntity.identityUserName == '' ? '请选择' : controller.jobPlanDetailsEntity.identityUserName!,
                    context,
                    callback: () {
                      controller.riskIdentificationPersonSelect();
                    },
                  ),
                  SizedBox(height: 70.w)
                ],
              ),
            ),
            bottomSheet: Container(
              width: double.infinity,
              height: 50.w,
              color: Colors.white,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Visibility(
                    visible: controller.jobPlanDetailsEntity.id!.isNotEmpty,
                    child: Container(
                      margin: EdgeInsets.only(left: 16.w),
                      width: 120.w,
                      child: ElevatedButton(
                          style: ElevatedButton.styleFrom(
                              backgroundColor: const Color(0xffFFFFFF),
                              shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(22.r),
                              ),
                              side: BorderSide(
                                color: const Color(0xff5C6DFF),
                                width: 1.w, // 设置边框线的宽度
                              )),
                          onPressed: controller.deletePlan,
                          child: Text(
                            '撤销',
                            style: TextStyle(
                              fontSize: 16.sp,
                              color: const Color(0xff5C6DFF),
                            ),
                          )),
                    ),
                  ),
                  Visibility(
                    visible: controller.jobPlanRequestController.isEnabled,
                    child: Expanded(
                      child: Container(
                        color: Colors.white,
                        padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 5.w),
                        child: ElevatedButton(
                          style: ElevatedButton.styleFrom(
                            backgroundColor: AppColors.blue_button,
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(22.r),
                            ),
                          ),
                          onPressed: controller.submit,
                          child: Text(
                            '提交',
                            style: TextStyle(
                              fontSize: 16.sp,
                              color: Colors.white,
                            ),
                          ),
                        ),
                      ),
                    ),
                  )
                ],
              ),
            ),
          );
        });
  }
}
